package org.fintool.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

import org.fintool.beans.StockInfoMaster;

public class StockMasterUtil {

	//Always use uppercase for goog
	public static final String ASX="ASX";

	public static final String ASX_FILE_NAME="ASXListedCompanies.csv";
	public static final String US_FILE_NAME="USListedCompanies.csv";
	public static final String INDIAN_FILE_NAME="NSEListedCompanies.csv";


	public static final String   FILE_PATH_REL="\\bin\\org\\fintool\\common";


	//--- Array of StockInfoMaster Data
	public static final  ArrayList asxStocks=new ArrayList();
	public static final  ArrayList usStocks=new ArrayList();
	public static final  ArrayList indianStocks=new ArrayList();

	//--------

	static{

		loadASXList();
		loadUSStocks();
		loadIndianStocks();

	}

	/**
	 * Load the arrayList ASX list
	 */
	private final static void loadASXList(){

		//load list here


		BufferedReader br=null;
		FileReader fr=null;
		String currentLine=null;
		String currentStrArr[]=null;
		String stockName=null;
		String description=null;
		StockInfoMaster stkMasterDataCurrent=null;
		try{


			fr=new FileReader(new File((new File("").getCanonicalPath())+FILE_PATH_REL+File.separatorChar+ASX_FILE_NAME));
			if(fr!=null){
				br=new BufferedReader(fr);
				if(br!=null){
					while((currentLine=br.readLine())!=null){
						//System.out.println(currentLine);
						if(currentLine.indexOf(',')>0){
						stkMasterDataCurrent=new StockInfoMaster(ASX);
						currentStrArr=currentLine.split(",");
						stkMasterDataCurrent.setName(currentStrArr[1]);
						stkMasterDataCurrent.setDescription(currentStrArr[0]);
						stkMasterDataCurrent.setAdditional(currentStrArr[2]);
						asxStocks.add(stkMasterDataCurrent);
						}



					}

				}


			}



		}catch (Exception e) {
			e.printStackTrace();

			System.out.println("Could not load asx list "+e.getLocalizedMessage());

			// TODO: handle exception
		}


	}

	/**
	 * US stocks
	 */
	private final static void loadIndianStocks(){

		//load list here


		BufferedReader br=null;
		FileReader fr=null;
		String currentLine=null;
		String currentStrArr[]=null;
		String stockName=null;
		String description=null;
		StockInfoMaster stkMasterDataCurrent=null;
		try{


			fr=new FileReader(new File((new File("").getCanonicalPath())+FILE_PATH_REL+File.separatorChar+INDIAN_FILE_NAME));
			if(fr!=null){
				br=new BufferedReader(fr);
				if(br!=null){
					while((currentLine=br.readLine())!=null){
						//System.out.println(currentLine);
						if(currentLine.indexOf(',')>0){
						stkMasterDataCurrent=new StockInfoMaster();
						currentStrArr=currentLine.split(",");
						stkMasterDataCurrent.setName(currentStrArr[0]);
						stkMasterDataCurrent.setExchange("NSE");
						stkMasterDataCurrent.setDescription(currentStrArr[1]);
						indianStocks.add(stkMasterDataCurrent);
						}



					}
				}


			}



		}catch (Exception e) {
			e.printStackTrace();

			System.out.println("Could not load Indian  list "+e.getLocalizedMessage());

			// TODO: handle exception
		}


	}

	private final static void loadUSStocks(){

		//load list here


		BufferedReader br=null;
		FileReader fr=null;
		String currentLine=null;
		String currentStrArr[]=null;
		String stockName=null;
		String description=null;
		StockInfoMaster stkMasterDataCurrent=null;
		try{


			fr=new FileReader(new File((new File("").getCanonicalPath())+FILE_PATH_REL+File.separatorChar+US_FILE_NAME));
			if(fr!=null){
				br=new BufferedReader(fr);
				if(br!=null){
					while((currentLine=br.readLine())!=null){
						//System.out.println(currentLine);
						if(currentLine.indexOf(',')>0){
						stkMasterDataCurrent=new StockInfoMaster();
						currentStrArr=currentLine.split(",");
						stkMasterDataCurrent.setName(currentStrArr[1]);
						stkMasterDataCurrent.setExchange(currentStrArr[0]);
						stkMasterDataCurrent.setDescription(currentStrArr[2]);
						usStocks.add(stkMasterDataCurrent);
						}



					}
				}


			}



		}catch (Exception e) {
			e.printStackTrace();

			System.out.println("Could not load indian  list "+e.getLocalizedMessage());

			// TODO: handle exception
		}


	}

	public static void main(String[] args) {
		loadASXList();

	}

}
